<Project Sdk="Microsoft.Build.Traversal">
  <Import Project="Sdk.props" Sdk="Microsoft.DotNet.SharedFramework.Sdk" />

  <PropertyGroup Condition="'$(ContinuousIntegrationBuild)' != 'true'">
    <!-- Create an intermediate runsettings file to enable VSTest discovery. -->
    <EnableRunSettingsSupport>true</EnableRunSettingsSupport>
    <CreateIntermediateRunSettingsFile>true</CreateIntermediateRunSettingsFile>
    <CreateVsCodeRunSettingsFile>true</CreateVsCodeRunSettingsFile>
  </PropertyGroup>

  <PropertyGroup>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <!--
    Explicitly build the externals.csproj project first to create the PlatformManifest for
    the targeting and runtimepack before the test runners which consume that asset are built.
  -->
  <ItemGroup>
    <ExternalsProject Include="externals.csproj" Condition="'$(BuildTargetFramework)' == '$(NetCoreAppCurrent)' or '$(BuildTargetFramework)' == ''" />
    <ProjectReference Include="@(ExternalsProject)" Condition="'$(MSBuildRestoreSessionId)' != ''" />

    <ProjectReference Include="$(CommonTestPath)AppleTestRunner\AppleTestRunner.csproj" Condition="'$(TargetOS)' == 'maccatalyst' or '$(TargetOS)' == 'ios' or '$(TargetOS)' == 'iossimulator' or '$(TargetOS)' == 'tvos' or '$(TargetOS)' == 'tvossimulator'"/>
    <ProjectReference Include="$(CommonTestPath)AndroidTestRunner\AndroidTestRunner.csproj" Condition="'$(TargetOS)' == 'android'" />
    <ProjectReference Include="$(CommonTestPath)WasmTestRunner\WasmTestRunner.csproj" Condition="'$(TargetOS)' == 'browser' or '$(TargetOS)' == 'wasi'" />
    <ProjectReference Include="$(MonoProjectRoot)wasm\symbolicator\WasmSymbolicator.csproj" Condition="'$(TargetOS)' == 'browser'" />

    <!-- needed to test workloads for wasm -->
    <ProjectReference Include="$(InstallerProjectRoot)pkg\sfx\Microsoft.NETCore.App\Microsoft.NETCore.App.Runtime.sfxproj"
                      Pack="true"
                      Condition="'$(TargetOS)' == 'browser' or '$(TargetOS)' == 'wasi'" />
  </ItemGroup>

  <ItemGroup Condition="'$(TestReadyToRun)' == 'true'">
    <ProjectReference Include="$(InstallerProjectRoot)pkg/sfx/Microsoft.NETCore.App/Microsoft.NETCore.App.Runtime.sfxproj" />
    <ProjectReference Include="$(InstallerProjectRoot)pkg/sfx/bundle/Microsoft.NETCore.App.Bundle.bundleproj" />
  </ItemGroup>

  <Import Project="$(RepositoryEngineeringDir)testing\wasm-provisioning.targets"
          Condition="'$(TargetOS)' == 'browser' and '$(ContinuousIntegrationBuild)' == 'true'" />

  <Import Project="$(RepositoryEngineeringDir)testing\wasi-provisioning.targets"
          Condition="'$(TargetOS)' == 'wasi' and '$(ContinuousIntegrationBuild)' == 'true'" />

  <Target Name="BuildExternalsProject"
          BeforeTargets="Build">
    <MSBuild Targets="Build"
             Projects="@(ExternalsProject)"
             Properties="$(TraversalGlobalProperties)" />
  </Target>

  <Target Name="CreateIntermediateRunSettingsFile"
          DependsOnTargets="GenerateRunSettingsFile"
          BeforeTargets="Build"
          Condition="'$(CreateIntermediateRunSettingsFile)' == 'true'" />

  <Target Name="GetSharedFrameworkRuntimeFiles">
    <ItemGroup>
      <ManualRuntimePackNativeFile Include="System.Security.Cryptography.Native.Android.so" />
      <ManualRuntimePackNativeFile Include="System.Security.Cryptography.Native.Android.dex" />
      <ManualRuntimePackNativeFile Include="System.Security.Cryptography.Native.Android.jar" />
      <ManualRuntimePackNativeFile Include="System.Security.Cryptography.Native.OpenSsl.so" />
      <ManualRuntimePackNativeFile Include="System.Security.Cryptography.Native.Apple.dylib" />
      <ManualRuntimePackNativeFile Include="System.Security.Cryptography.Native.OpenSsl.dylib" />
      <SharedFrameworkRuntimeFile Include="$(MicrosoftNetCoreAppRuntimePackRidLibTfmDir)*;
                                           $(MicrosoftNetCoreAppRuntimePackNativeDir)*;
                                           @(ManualRuntimePackNativeFile->'$(MicrosoftNetCoreAppRuntimePackNativeDir)%(Identity)')"
                                  TargetPath="runtimes/" />
    </ItemGroup>
  </Target>

  <!-- Generate the runtime pack's PlatformManifest -->
  <UsingTask TaskName="GenerateFileVersionProps" AssemblyFile="$(InstallerTasksAssemblyPath)"/>
  <Target Name="GenerateFileVersionPropsRuntimePack"
          DependsOnTargets="GetSharedFrameworkRuntimeFiles"
          AfterTargets="BuildExternalsProject"
          Inputs="@(SharedFrameworkRuntimeFile)"
          Outputs="$(MicrosoftNetCoreAppRuntimePackDir)data\PlatformManifest.txt"
          Condition="'$(BuildTargetFramework)' == '$(NetCoreAppCurrent)' or '$(BuildTargetFramework)' == ''">
    <GenerateFileVersionProps Files="@(SharedFrameworkRuntimeFile)"
                              PackageId="$(MicrosoftNetCoreAppFrameworkName).Runtime.$(OutputRID)"
                              PackageVersion="$(ProductVersion)"
                              PlatformManifestFile="$(MicrosoftNetCoreAppRuntimePackDir)data\PlatformManifest.txt"
                              PreferredPackages="$(MicrosoftNetCoreAppFrameworkName).Runtime.$(OutputRID)"
                              PermitDllAndExeFilesLackingFileVersion="true" />
  </Target>

  <!-- Generate the ref pack's PlatformManifest -->
  <Target Name="GenerateFileVersionPropsRefPack"
          DependsOnTargets="GetSharedFrameworkRuntimeFiles"
          AfterTargets="BuildExternalsProject"
          Inputs="@(SharedFrameworkRuntimeFile)"
          Outputs="$(MicrosoftNetCoreAppRefPackDataDir)PlatformManifest.txt"
          Condition="'$(BuildTargetFramework)' == '$(NetCoreAppCurrent)' or '$(BuildTargetFramework)' == ''">
    <GenerateFileVersionProps Files="@(SharedFrameworkRuntimeFile)"
                              PackageId="$(MicrosoftNetCoreAppFrameworkName).Ref"
                              PackageVersion="$(ProductVersion)"
                              PlatformManifestFile="$(MicrosoftNetCoreAppRefPackDataDir)PlatformManifest.txt"
                              PreferredPackages="$(MicrosoftNetCoreAppFrameworkName).Ref"
                              PermitDllAndExeFilesLackingFileVersion="true" />
  </Target>

  <!-- Generate the shared framework's deps.json -->
  <UsingTask TaskName="GenerateTestSharedFrameworkDepsFile" AssemblyFile="$(InstallerTasksAssemblyPath)"/>
  <Target Name="GenerateTestSharedFrameworkAssets"
          AfterTargets="BuildExternalsProject"
          Inputs="$(NetCoreAppCurrentTestHostSharedFrameworkPath)*.*"
          Outputs="$(NetCoreAppCurrentTestHostSharedFrameworkPath)$(MicrosoftNetCoreAppFrameworkName).deps.json"
          Condition="'$(BuildTargetFramework)' == '$(NetCoreAppCurrent)' or '$(BuildTargetFramework)' == ''">
    <!-- Shared framework deps file generation. Produces a test shared-framework deps file. -->
    <GenerateTestSharedFrameworkDepsFile SharedFrameworkDirectory="$(NetCoreAppCurrentTestHostSharedFrameworkPath)"
                                         RuntimeGraphFiles="$([MSBuild]::ValueOrDefault('$(RuntimeIdentifierGraphPath)', '$(BundledRuntimeIdentifierGraphFile)'))"
                                         TargetRuntimeIdentifier="$(OutputRID)" />
  </Target>

  <Target Name="GetRuntimePackFiles">
    <ItemGroup>
      <RuntimePackLibFile Include="$(MicrosoftNetCoreAppRuntimePackRidLibTfmDir)*.*">
        <TargetPath>runtimes/$(OutputRID)/lib/$(NetCoreAppCurrent)</TargetPath>
      </RuntimePackLibFile>
      <RuntimePackNativeFile Include="$(MicrosoftNetCoreAppRuntimePackNativeDir)*.*">
        <TargetPath>runtimes/$(OutputRID)/native</TargetPath>
        <IsNative>true</IsNative>
      </RuntimePackNativeFile>
      <!-- Clear the IsNative flag on System.Private.CoreLib given that it is present in native dir but it is actually managed -->
      <RuntimePackNativeFile IsNative=""
                             Condition="'%(FileName)%(Extension)' == 'System.Private.CoreLib.dll'" />
    </ItemGroup>
    <!-- We need to set this metadata in a separate ItemGroup than when the Items are initially populated in order to
    have access to the Extension metadata. -->
    <ItemGroup>
      <RuntimePackLibFile>
        <IsSymbolFile Condition="'%(Extension)' == '.pdb'">true</IsSymbolFile>
      </RuntimePackLibFile>
      <RuntimePackNativeFile>
        <IsSymbolFile Condition="'%(Extension)' == '.pdb'">true</IsSymbolFile>
      </RuntimePackNativeFile>
    </ItemGroup>
  </Target>

  <!-- Generate the runtime pack's RuntimeList.xml -->
  <UsingTask TaskName="CreateFrameworkListFile" AssemblyFile="$(DotNetSharedFrameworkTaskFile)"/>
  <Target Name="GenerateRuntimeListFile"
          DependsOnTargets="GetRuntimePackFiles"
          AfterTargets="BuildExternalsProject"
          Inputs="@(RuntimePackLibFile);@(RuntimePackNativeFile)"
          Outputs="$(MicrosoftNetCoreAppRuntimePackDir)data\RuntimeList.xml"
          Condition="'$(BuildTargetFramework)' == '$(NetCoreAppCurrent)' or '$(BuildTargetFramework)' == ''">
    <ItemGroup>
      <FrameworkListRootAttribute Include="Name" Value="$(NetCoreAppCurrentBrandName)" />
      <FrameworkListRootAttribute Include="TargetFrameworkIdentifier" Value="$(NetCoreAppCurrentIdentifier)" />
      <FrameworkListRootAttribute Include="TargetFrameworkVersion" Value="$(NetCoreAppCurrentVersion)" />
      <FrameworkListRootAttribute Include="FrameworkName" Value="$(MicrosoftNetCoreAppFrameworkName)" />
    </ItemGroup>

    <CreateFrameworkListFile Files="@(RuntimePackLibFile);@(RuntimePackNativeFile)"
                             TargetFile="$(MicrosoftNetCoreAppRuntimePackDir)data\RuntimeList.xml"
                             TargetFilePrefixes="ref/;runtimes/"
                             RootAttributes="@(FrameworkListRootAttribute)" />
  </Target>

  <Import Project="Sdk.targets" Sdk="Microsoft.DotNet.SharedFramework.Sdk" />
</Project>
